# Copyright (c) 2008-2009 Mikhail Chernov 
#
# This file is part of dcExplorer.
#
# dcExplorer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# dcExplorer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with dcExplorer.  If not, see <http://www.gnu.org/licenses/>.

# Scripting class with extensions
package dcExplorer::core::Planet;
use strict;
use utf8;
use Settings;
use core::Object;
use base 'dcExplorer::core::Object';
use WeakRef;
use IO::Seekable qw(SEEK_SET);
use server::Command;

# corruption is implemented but not documented;
# hidden shows if a planet is closed to the allies;
@dcExplorer::core::Planet::props = 
	qw(o e m t s name image img_surface fake_info population free_pepl x y jumpable owner_id owner_name age my popgrowth main_mining second_mining money_mining coment icon corruption hidden);

# Construction methods

sub new 
{
	my $proto = shift;
	my $class = ref($proto) || $proto;
	my $self = $class->SUPER::new;
	weaken($self->{Parent} = shift);
	my $x = shift;
	my $y = shift;
	$self->{'x'} = $x, $self->{'y'} = $y, $self->{'name'} = "N".$x.":".$y if defined($x) && defined($y);

	# TODO: Remove this quick and dirty hack!
	open SIZES, "<".$Settings::DataPath."images.bin";
	binmode SIZES;
	my $byte = ($x - 1) * 4000 + int(($y - 1)*2);
	seek SIZES, $byte, SEEK_SET;
	my $packed;
	sysread SIZES, $packed, 2;
	my $unpacked = unpack ('n', $packed);

	$self->{'image'} = $unpacked;
	$self->{'img_surface'} = $unpacked % 10;

	bless ($self, $class);
	return $self;
}

# Internal engine methods

sub getEmpire
{
	my $self = shift;
	return $self->{Parent}->getEmpire();
}

# Extended scripting interface

sub changeName
{
	my $self = shift;
	return unless $self->getProp('my');
	my $name = shift;
	$self->setProp('name', $name);
	$self->getEmpire()->commandQueue()->enqueue(dcExplorer::server::Command->new('change_planet_name', {'planetid' => $self->getProp('x').":".$self->getProp('y'), 'newname' => $name}));
}

sub changePermissions
{
	my $self = shift;
	return unless $self->getProp('my');
	my $shared = shift;
	$self->setProp('hidden', 1 - $shared);
	$self->getEmpire()->commandQueue()->enqueue(dcExplorer::server::Command->new('change_planet_perms', {'planetid' => $self->getProp('x').":".$self->getProp('y'), 'shared' => $shared}));
}

sub addPermissions
{
	my $self = shift;
	return unless $self->getProp('my');
	my $morePlayers = shift;
	$self->getEmpire()->commandQueue()->enqueue(dcExplorer::server::Command->new('all_ext_planet_perms', {'planetid' => $self->getProp('x').":".$self->getProp('y'), 'more_players' => $morePlayers}));
}

sub sharePlanetManagement
{
	my $self = shift;
	return unless $self->getProp('my');
	my $morePlayers = shift;
	$self->getEmpire()->commandQueue()->enqueue(dcExplorer::server::Command->new('share_planet_management', {'planetid' => $self->getProp('x').":".$self->getProp('y'), 'more_players' => $morePlayers}));
}

sub removePermissions
{
	my $self = shift;
	return unless $self->getProp('my');
	my $playerId = shift;
	$self->getEmpire()->commandQueue()->enqueue(dcExplorer::server::Command->new('remove_ext_permission', {'planetid' => $self->getProp('x').":".$self->getProp('y'), 'player_id' => $playerId}));
}

1;
